﻿//=====================================================================================================
// LocalizationLibrary
// The Localization Library is a collection of reusable software component that provide 
// support for localization. This library enable you to localize WPF, Silverlight and WP7 applications.
// (http://localizationlibrary.codeplex.com)
//=====================================================================================================
// Author: Nicola Gallo (Website: www.nicola.gallo.name, Email: nicolagmt@hotmail.it) 
//=====================================================================================================

namespace HelloWorld.Startup.Localization
{
    //.NET References
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    //LocalizationLibrary References
    using LocalizationLibrary;

    // HelloWorld References
    using HelloWorld.Startup.BingTranslatorService;

    /// <summary>
    /// Defines a <see cref="LocalizationProvider"/> that use the bing web service for the translation.
    /// </summary>
    public class BingLocalizationProvider : LocalizationProvider
    {
        //Constructors

        /// <summary>
        /// Creates a new instance of <see cref="LocalizationProvider"/> class.
        /// </summary>
        /// <param name="token">Token to localize.</param>
        /// <param name="onLocalization">Callback to call when the <see cref="LocalizationProvider"/> has completed the localization.</param>
        /// <exception cref="ArgumentNullcExeption">
        /// Thrown whether <paramref name="token"/> parameter is null.
        /// </exception>
        /// <exception cref="ArgumentNullcExeption">
        /// Thrown whether <paramref name="onLocalization"/> parameter is null.
        /// </exception>
        public BingLocalizationProvider(DynamicResourceToken token, 
            LocalizationCompletionCallback onLocalization) : base(token, onLocalization) { }

        //Methods

        /// <summary>
        /// Localize the input <paramref name="dynamicResourceToken"/> for the input <paramref name="language"/>.
        /// </summary>
        public override void Localize()
        {
            LanguageServiceClient client = new LanguageServiceClient();
            DynamicResourceDescription dRDescription = LocalizationManager.GetResourceDescription(this.Token.ResourceID);
            Language[] languages = dRDescription.GetLocalizedLanguages();
            if (languages.Length > 0)
            {
                String fromLanguage = languages[0].Code;
                String toLanguage = this.Token.Language.Code;
                String text = dRDescription[languages[0]] as String;
                String appID = "C865D3114916BF95BFAEA32C60EF75D0B2D1AF07";
                this.Token.Value = client.Translate(appID, text, fromLanguage, toLanguage);
            }
            this.PublishLocalization();
        }
    }
}
